#include "ESceneManager.h"
#include "ESceneObject.h"
#include "EScreen.h"
#include "EDebug.h"

#include "eCPP.h"
namespace engine
{
//------------------------------------------------------------
ESceneManager::ESceneManager() :
m_pOgreSceneManager( NULL ),
m_pCurrentScene( NULL )
{
}

//------------------------------------------------------------
ESceneManager::~ESceneManager()
{
  ESceneMap::iterator it;
  for( it = m_mapScenes.begin(); it != m_mapScenes.end(); it++ )
  {
    delete it->second;
  }
  m_mapScenes.clear();
}

//------------------------------------------------------------
void ESceneManager::setOgreSceneManager( SceneManager& sceneManager )
{
  m_pOgreSceneManager = &sceneManager;
#ifdef eDEBUG_RENDER
  g_debugRenderer.setCurrentOgreSceneManager( &sceneManager );
#endif
}

//------------------------------------------------------------
/// \todo remove old screen from the ogre scene manager
bool ESceneManager::setCurrentScene( const string& sName )
{
  if( sName == "" )
  {
    m_pCurrentScene = NULL;
    return true;
  }
  ESceneMap::iterator it = m_mapScenes.find( sName );
  if( it == m_mapScenes.end() )
    return false;

  m_pCurrentScene = it->second;
  cASSERT( m_pCurrentScene != NULL );
  m_pCurrentScene->updateParams();
  return true;
}

//------------------------------------------------------------
bool ESceneManager::update ( const EGameTime& time )
{
  if( m_pCurrentScene != NULL )
    cTEST( m_pCurrentScene->update( time ) );
  cTEST( EEntityManager::update( time ) );
  return true;
}

//------------------------------------------------------------
bool ESceneManager::startGame ()
{
  if( m_pCurrentScene != NULL )
    cTEST( m_pCurrentScene->startGame( ) );
  cTEST( EEntityManager::startGame() );
  return true;
}

//------------------------------------------------------------
bool ESceneManager::updateGame(const EGameTime& time )
{
  if( m_pCurrentScene != NULL )
    cTEST( m_pCurrentScene->updateGame( time ) );
  cTEST( EEntityManager::updateGame( time ) );
  return true;
}

//------------------------------------------------------------
bool ESceneManager::endGame ()
{
  cTEST( EEntityManager::endGame() );
  if( m_pCurrentScene != NULL )
    cTEST( m_pCurrentScene->endGame( ) );
  return true;
}

//------------------------------------------------------------
EScene* ESceneManager::createScene(const string& sName, const ESceneDesc& sceneDesc )
{
  cASSERT( m_pOgreSceneManager != NULL );
  if( !sceneDesc.isValid() )
    return NULL;
  EScene* pScene = new EScene( sceneDesc, *m_pOgreSceneManager );
  cASSERT( pScene != NULL );
  m_mapScenes[ sName ] = pScene;

  return pScene;
}

//------------------------------------------------------------
void ESceneManager::destroyScene(const string& sName )
{
  ESceneMap::iterator it = m_mapScenes.find( sName );
  cASSERT( it != m_mapScenes.end() );
  delete it->second;
  m_mapScenes.erase( sName );
}

} // end namespace